package com.bwie.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwie.topnews.R;
import com.bwie.utils.CommonUtils;

/**
 * Created by PC on 2016/11/1.
 */
public class ViewPagerIndicator extends HorizontalScrollView implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private LinearLayout linearlayout;

    public void init() {

    }

    public ViewPagerIndicator(Context context) {
        super(context);
        init();
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    public void setViewPager(ViewPager vp)
    {
        this.viewPager=vp;

        vp.setOnPageChangeListener(this);
        PagerAdapter adapter=vp.getAdapter();
        linearlayout = new LinearLayout(getContext());
        for (int i = 0; i < adapter.getCount(); i++) {
            View view= CommonUtils.inflate(R.layout.indicator_item);
            view.setOnClickListener(this);
            TextView tv = (TextView) view.findViewById(R.id.tv_indicator_title);
            TextView icon = (TextView) view.findViewById(R.id.tv_indicator_icon);

            if (i==0)
            {
                icon.setVisibility(View.VISIBLE);
                tv.setTextColor(Color.RED);
            }
            else
            {
                icon.setVisibility(View.INVISIBLE);
                tv.setTextColor(Color.BLACK);
            }
            System.out.println(adapter.getPageTitle(i)+"====");
            tv.setText(adapter.getPageTitle(i));
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            params.setMargins(CommonUtils.dip2px(5),CommonUtils.dip2px(5),CommonUtils.dip2px(5),CommonUtils.dip2px(5));
            linearlayout.addView(view,params);
        }
        this.addView(linearlayout);
    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < linearlayout.getChildCount(); i++) {
            View view1=linearlayout.getChildAt(i);
            TextView tv = (TextView) view1.findViewById(R.id.tv_indicator_title);
            TextView icon = (TextView) view1.findViewById(R.id.tv_indicator_icon);
            if (v==view1)
            {
                viewPager.setCurrentItem(i);
                tv.setTextColor(Color.RED);
                icon.setVisibility(View.VISIBLE);
            }
            else
            {
                tv.setTextColor(Color.BLACK);
                icon.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < linearlayout.getChildCount(); i++) {
            View view1=linearlayout.getChildAt(i);
            TextView tv = (TextView) view1.findViewById(R.id.tv_indicator_title);
            TextView icon = (TextView) view1.findViewById(R.id.tv_indicator_icon);
            if (position==i)
            {
                this.scrollTo((int) (view1.getX()-linearlayout.getChildAt(0).getWidth()),0);
                tv.setTextColor(Color.RED);
                icon.setVisibility(View.VISIBLE);
            }
            else
            {
                tv.setTextColor(Color.BLACK);
                icon.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}

